package com.caption.config;

import lombok.Data;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;

/**
 * elasticsearch 客户端配置
 */
@ConfigurationProperties(prefix = "elasticsearch")
@Configuration
@Data
public class ElasticsearchConfig extends AbstractElasticsearchConfiguration {

    private String url;

    @Override
    public RestHighLevelClient elasticsearchClient() {
        String[] datas = url.split(",");
        HttpHost[] hosts = new HttpHost[datas.length];
        for(int i=0;i<datas.length;i++){
            String[] strs = datas[i].split(":");
            HttpHost host =new HttpHost(strs[0],Integer.valueOf(strs[1]));
            hosts[i] = host;
        }
        RestClientBuilder builder = RestClient.builder(hosts);
        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(builder);
        return restHighLevelClient;
    }


/*    @Override
    public RestHighLevelClient elasticsearchClient() {
        HttpHost[] data = {
                new HttpHost("192.168.1.121", 9200),
                new HttpHost("192.168.1.122", 9200),
                new HttpHost("192.168.1.123", 9200)
        };
        RestClientBuilder builder = RestClient.builder(data);
        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(builder);
        return restHighLevelClient;
    }*/



}
